package com.sun.common.recyclerview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.sun.dailyprj.R;
import com.sun.dailyprj.zotye.viewmodel.UpgradeHeaderHolder;
import com.sun.dailyprj.zotye.viewmodel.UpgradeViewHolder;

public abstract class BaseBindingAdapter<M, B extends ViewDataBinding> extends RecyclerView.Adapter{

    protected Context context;
    protected ObservableArrayList<M> items;
    protected ObservableArrayList<M> header;
    protected ListChangedCallback itemsChangeCallback;
    protected int TYPE_HEADER = 1001;

    public BaseBindingAdapter(Context context) {
        this.context = context;
        this.items = new ObservableArrayList<>();
        this.header = new ObservableArrayList<>();
        this.itemsChangeCallback = new ListChangedCallback();
    }

    public ObservableArrayList<M> getItems() {
        return items;
    }

    public ObservableArrayList<M> getHeader() {
        return header;
    }

    @Override
    public int getItemCount() {
        return items.size() + header.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.i("syp", "viewType:" + viewType);
        if(viewType == TYPE_HEADER) {
            B binding = DataBindingUtil.inflate(LayoutInflater.from(this.context),
                    this.getLayoutResId(viewType), parent, false);
            return new UpgradeHeaderHolder(binding.getRoot());
//            return new BaseBindingViewHolder(binding.getRoot());
        }
        B binding = DataBindingUtil.inflate(LayoutInflater.from(this.context), this.getLayoutResId(viewType), parent, false);
//        return new BaseBindingViewHolder(binding.getRoot());
        return new UpgradeViewHolder(binding.getRoot());
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) {
            return TYPE_HEADER;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == 0 && header.size() > 0) {
            holder = new UpgradeHeaderHolder(holder.itemView);
            B binding = DataBindingUtil.getBinding(holder.itemView);
            this.onBindItem(binding, this.header.get(position),position);
        } else {
            holder = new UpgradeViewHolder(holder.itemView);
            B binding = DataBindingUtil.getBinding(holder.itemView);
            this.onBindItem(binding, this.items.get(position-1),position-1);
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.items.addOnListChangedCallback(itemsChangeCallback);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.items.removeOnListChangedCallback(itemsChangeCallback);
    }

    //region 处理数据集变化
    protected void onChanged(ObservableArrayList<M> newItems) {
        resetItems(newItems);
        notifyDataSetChanged();
    }

    protected void onItemRangeChanged(ObservableArrayList<M> newItems, int positionStart, int itemCount) {
        resetItems(newItems);
        notifyItemRangeChanged(positionStart, itemCount);
    }

    protected void onItemRangeInserted(ObservableArrayList<M> newItems, int positionStart, int itemCount) {
        resetItems(newItems);
        notifyItemRangeInserted(positionStart, itemCount);
    }

    protected void onItemRangeMoved(ObservableArrayList<M> newItems) {
        resetItems(newItems);
        notifyDataSetChanged();
    }

    protected void onItemRangeRemoved(ObservableArrayList<M> newItems, int positionStart, int itemCount) {
        resetItems(newItems);
        notifyItemRangeRemoved(positionStart, itemCount);
    }

    protected void resetItems(ObservableArrayList<M> newItems) {
        this.items = newItems;
    }

    //endregion
    protected abstract @LayoutRes
    int getLayoutResId(int viewType);

    protected abstract void onBindItem(B binding, M item,int position);

    //item里面有多个控件可以点击（item+item内部控件）
    public enum ViewName{
        ITEM,
        PRACTISE
    }


    public interface OnItemClickListener{
        void onItemClick(View v, ViewName viewName, int position);

        void onItemLongClick(View v);
    }

    protected OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class ListChangedCallback extends ObservableArrayList.OnListChangedCallback<ObservableArrayList<M>>{
        @Override
        public void onChanged(ObservableArrayList<M> newItems) {
            BaseBindingAdapter.this.onChanged(newItems);
        }

        @Override
        public void onItemRangeChanged(ObservableArrayList<M> newItems, int i, int i1) {
            BaseBindingAdapter.this.onItemRangeChanged(newItems, i, i1);
        }

        @Override
        public void onItemRangeInserted(ObservableArrayList<M> newItems, int i, int i1) {
            BaseBindingAdapter.this.onItemRangeInserted(newItems, i, i1);
        }

        @Override
        public void onItemRangeMoved(ObservableArrayList<M> newItems, int i, int i1, int i2) {
            BaseBindingAdapter.this.onItemRangeMoved(newItems);
        }

        @Override
        public void onItemRangeRemoved(ObservableArrayList<M> sender, int positionStart, int itemCount) {
            BaseBindingAdapter.this.onItemRangeRemoved(sender, positionStart, itemCount);
        }
    }
}
